import * as PIXI from 'pixi.js';
import { getTextureFromCache } from '../utils';
import ControlBtn from '../../../../img/control.png';

const name = 'controlIcon';

/**
 * 控制按钮，可以通过拖拽的方式放缩、旋转
 *
 * @export
 * @class ControlIcon
 */
export default class ControlIcon {
  icon;

  constructor(app, obj) {
    this.app = app;
    this.obj = obj;
  }

  /**
   * 创建一个控制按钮
   *
   * @return {*}
   * @memberof ControlIcon
   */
  create() {
    const texture = getTextureFromCache(this.app, name, ControlBtn); // 优先从预加载缓存中取出
    const sprite = PIXI.Sprite.from(texture);
    sprite.width = 21;
    sprite.height = 21;
    sprite.anchor.set(0.5);

    const rect = this.obj.getBounds();
    const { x, y, width } = rect;
    sprite.x = x + width;
    sprite.y = y;

    this.icon = sprite;
    sprite.name = 'ctrlBtn';
    return this.icon;
  }
}
